﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李刚 
    //
    // 文件名：WebHint.cs
    // 文件功能描述：当前用户信息的管理，页面跳转等
    // 创建标识：李刚 2009-03-23
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.Web
{
    public class CurrentAccount
    {
        //系统Session Key常量 
        private const string ACCOUNT = "ACCOUNT_KEY";
        private const string VALIDATE_CODE = "NUM_CODE";

        //系统超级管理员,该用户用户最高权限且不允许删除
        private const string SUPER_ADMIN = "ROOT";

        //常用的功能页面地址
        private const string URL_INDEX = "~/Default.aspx";
        private const string URL_SIGNIN = "~/UserLogin.aspx";
        private const string URL_TIMEOUT = "~/CommonPage/TimeoutPage.aspx";
        private const string URL_NO_PERMISSION = "~/CommonPage/NoPermission.aspx";

        //各用户类型默认的内容主页
        public const string URL_DEFAULT_INDEX_URL = "~/CommonPage/defaultContent.aspx";
        public const string URL_CENTER_INDEX_URL = "~/Admin/defaultContent.aspx";
        public const string URL_HOSPITAL_INDEX_URL = "~/Hospitals/defaultContent.aspx";
        public const string URL_ENTERPRISE_INDEX_URL = "~/Enterprise/defaultContent.aspx";


        static CurrentAccount()
        {


        }

        /// <summary>
        /// 获取当前用户的Id
        /// </summary>
        public static string UserID
        {
            get
            {
                return GetUser().UserID;
            }
        }

        /// <summary>
        /// 获取当前用户的用户名
        /// </summary>
        public static string UserName
        {
            get
            {
                return GetUser().UserName;
            }
        }

        /// <summary>
        /// 当前登录用户的IP
        /// </summary>
        public static string ClientIP
        {
            get
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
        }

        /// <summary>
        /// 是否超时
        /// </summary>
        /// <returns></returns>
        public static bool IsTimeout()
        {
        //    默认登陆用户 仅在开发模式中使用 生产环境应将其去掉
            //Model.Org org = new HZ.DrugTrade.Model.Org();
            //org.OrgID = "h01";
            //org.OrgName = "江苏省人民医疗机构";
            //Model.Role role = new HZ.DrugTrade.Model.Role();
            //role.RoleID = "01";
            //role.RoleName = "医疗机构";
            //HZ.DrugTrade.Model.User model = new HZ.DrugTrade.Model.User();
            //model.Org = org;
            //model.UserRole = role;
            //model.UserID = "y001";
            //model.UserName = "江苏省人民医疗机构";

            //Model.Org org = new HZ.DrugTrade.Model.Org();
            //org.OrgID = "01";
            //org.OrgName = "江苏华招医药有限公司";
            //Model.Role role = new HZ.DrugTrade.Model.Role();
            //role.RoleID = "02";
            //role.RoleName = "配送企业";
            //HZ.DrugTrade.Model.User model = new HZ.DrugTrade.Model.User();
            //model.Org = org;
            //model.UserRole = role;
            //model.UserID = "P001";
            //model.UserName = "江苏华招医药有限公司";
            //if (HttpContext.Current.Session[ACCOUNT] == null)
            //{
            //    HttpContext.Current.Session[ACCOUNT] = model;
            //}

            HZ.DrugTrade.Model.User myAccount = (HZ.DrugTrade.Model.User)HttpContext.Current.Session[ACCOUNT];
            if (myAccount == null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取当前用户实体
        /// </summary>
        /// <returns></returns>
        public static HZ.DrugTrade.Model.User GetUser()
        {

            HZ.DrugTrade.Model.User myAccount = (HZ.DrugTrade.Model.User)HttpContext.Current.Session[ACCOUNT];

            if (myAccount == null)
            {

                Goto_Timeout();
                return null;
            }
            return myAccount;

        }

        /// <summary>
        /// 将当前登陆用户保存到Session
        /// </summary>
        /// <param name="model"></param>
        public static void SetUser(HZ.DrugTrade.Model.User model)
        {

            if (model.UserRole.RoleID == "05" || model.UserRole.RoleID == "02")
            {
                model.Org.Area.AreaID = Constants.CURRENT_DEPLOY_AREAID;
            }
            HttpContext.Current.Session[ACCOUNT] = model;
        }
        /// <summary>
        /// 设置当前登录账户Session
        /// </summary>
        public static User Account
        {
            get
            {
                return HttpContext.Current.Session[ACCOUNT] as User;
            }

            set
            {
                HttpContext.Current.Session[ACCOUNT] = value;

            }
        }
        /// <summary>
        /// 退出登陆
        /// </summary>
        public static void Logout()
        {
            //
            HttpContext.Current.Session.Clear();
            //
            HttpContext.Current.Session.Abandon();
        }

        /// <summary>
        /// 重新登陆
        /// </summary>
        public static void Goto_Login()
        {
            Logout();
            //清除缓存
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.CacheControl = "no-cache";

            //跳转到登陆页面

            // BLL层缓存清除
            IBLL.IBLL_Login ibl = HZ.DrugTrade.BLLFactory.Factory.BLL_Login();
            ibl.Logoff();
            HttpContext.Current.Response.Redirect(URL_SIGNIN, true);
        }

        /// <summary>
        /// 判断当前用户是否系统超级管理员
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public static bool IsSuperAdmin()
        {
            if (GetUser().UserID.ToLower() == SUPER_ADMIN.ToLower())
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断给定用户是否系统超级管理员
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static bool IsSuperAdmin( string userID )
        {
            if (userID.ToLower() == SUPER_ADMIN.ToLower())
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 跳转到没有权限页面
        /// </summary>
        public static void Goto_NoPermission()
        {
            HttpContext.Current.Response.Redirect(URL_NO_PERMISSION, true);
        }

        /// <summary>
        /// 跳转到超时页面
        /// </summary>
        public static void Goto_Timeout()
        {
            //清除缓存
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.CacheControl = "no-cache";
            //
            HttpContext.Current.Response.Redirect(URL_TIMEOUT, true);
        }

        /// <summary>
        /// 验证码校验函数
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool VerifyValidateCode(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                if (HttpContext.Current.Session[VALIDATE_CODE] != null)
                {
                    if (HttpContext.Current.Session[VALIDATE_CODE].ToString() == code)
                    {
                        //验证成功清空Session
                        HttpContext.Current.Session[VALIDATE_CODE] = null;
                        return true;
                    }
                    
                }
            }
            //验证失败清空Session
            HttpContext.Current.Session[VALIDATE_CODE] = null;
            return false;
        }

        /// <summary>
        /// 设置验证码
        /// </summary>
        /// <param name="code"></param>
        public static void SetValidateCode( string code)
        {
            HttpContext.Current.Session[VALIDATE_CODE] = code;
        }

        /// <summary>
        /// 获取当前部署的区域
        /// </summary>
        /// <returns></returns>
        public static Area GetCurrentDeployArea()
        {
            IBLL.IBLL_Area areaCtrl = HZ.DrugTrade.BLLFactory.Factory.BLL_Area();
            return areaCtrl.GetArea( Constants.CURRENT_DEPLOY_AREAID );
        }
    }
}
